package com.template.web.controller;

import com.template.web.constants.Constant;
import com.template.web.utils.JsonResult;
import com.template.web.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class LoginController {

    @RequiresPermissions("perm:test:1")
    @GetMapping("/index")
    public JsonResult<Boolean> index() {
        Object o = SecurityUtils.getSubject().getPrincipal();
        log.info("obj {}", o);
        return JsonResult.success();
    }

    @PostMapping("/login")
    public JsonResult<String> login(@RequestParam("username") String username, @RequestParam("password") String password) {
        // 校验用户信息
        // UnknownAccountException
        // LockedAccountException
        String token = JwtUtil.sign("1", username);
        return JsonResult.success(token);
    }

    @PostMapping("/logout")
    public JsonResult<String> logout(@RequestHeader(Constant.TOKEN_HEADER) String token) {
        // 删除redis缓存中的token
        return JsonResult.success("注销成功");
    }
}
